//
//  EMSTrainingQueueProgressBar.m
//  EVE Mac Suite
//
//  Created by Vadim Lozko on 4/10/09.
//  Copyright 2009  __Vadim__. All rights reserved.
//

#import "EMSTrainingQueueProgressBar.h"
#import "EMSSkillTrainingInfo.h"
#import "EMSGeneralUtils.h"

@implementation EMSTrainingQueueProgressBar

- (id)initWithFrame:(NSRect)frame {
    if(self = [super initWithFrame:frame])
	{
		queuedItems = [[NSMutableArray alloc] init];
    }
    return self;
}

-(void)dealloc
{
	[queuedItems release];
	[super dealloc];
}

- (void)drawRect:(NSRect)rect 
{
	int i;
	float bottom = rect.origin.y;
	float top = rect.origin.y + rect.size.height;
	float drawboxBottom = bottom + 20;
	float left = rect.origin.x;
	float right = rect.origin.x + rect.size.width;
	float drawboxRight = right - 10;
	float drawboxWidth = drawboxRight - left;
	float drawboxHeight = top - drawboxBottom;
	float drawboxHeightMiddle = drawboxBottom + (drawboxHeight / 2);
	if(queuedItems && [queuedItems count] > 0)
	{

		
		[[NSColor blueColor] set];
		BOOL alternateColor = NO;
		//NSManagedObject* character = [[queuedItems objectAtIndex:0] valueForKey:@"queuedFor"];
		
		double endPositionTracker = 0;
		//draw subboxes
		for(i = 0; i < [queuedItems count]; i++)
		{
			/*NSDictionary* displayInfo = [[queuedItems objectAtIndex:i] valueForKeyPath:@"queuedItem.displayInfo"];
			TrainingQueuePercentPosition position =  [SkillTrainingInfo TrainingQueuePercentPositionForQueuedItems:queuedItems 
																										  itemInfo:displayInfo 
																										 character:character];*/
			
			NSTimeInterval trainingTime = [[[queuedItems objectAtIndex:i] valueForKey:@"trainingTime"] doubleValue];
			double percentOf24Hours = trainingTime / 86400;
			
			float leftStart = endPositionTracker;//position.start * drawboxWidth;
			float rightEnd = endPositionTracker + percentOf24Hours * drawboxWidth;//position.end * drawboxWidth;
			if(rightEnd > right)
			{
				NSBezierPath* trianglePath = [NSBezierPath bezierPath];
				[trianglePath moveToPoint:NSMakePoint(drawboxRight, top)];
				[trianglePath lineToPoint:NSMakePoint(right,drawboxHeightMiddle)];
				[trianglePath lineToPoint:NSMakePoint(drawboxRight, drawboxBottom)];
				[trianglePath closePath];
				[trianglePath fill];
				rightEnd = drawboxRight;
			}
			float subBoxWidth = rightEnd - leftStart;
			//[NSBezierPath fillRect:NSMakeRect(leftStart, top, subBoxWidth, drawboxHeight)];
			[NSBezierPath fillRect:NSMakeRect(leftStart, drawboxBottom, subBoxWidth, drawboxHeight)];
			
			/*if(i == [queuedItems count] - 1 && percentOf24Hours > 1)
			{
				NSBezierPath* trianglePath = [NSBezierPath bezierPath];
				[trianglePath moveToPoint:NSMakePoint(drawboxRight, top)];
				[trianglePath lineToPoint:NSMakePoint(right,drawboxHeightMiddle)];
				[trianglePath lineToPoint:NSMakePoint(drawboxRight, drawboxBottom)];
				[trianglePath closePath];
				[trianglePath fill];
			}*/
			alternateColor = (alternateColor) ? NO : YES;
			if(alternateColor)
			{
				[[NSColor cyanColor] set];
			} else {
				[[NSColor blueColor] set];
			}
			endPositionTracker = rightEnd;
		}
	}
	[[NSColor blackColor] set];
	[NSBezierPath setDefaultLineWidth:1.0];
	[[NSGraphicsContext currentContext] setShouldAntialias:NO];
	float barSpacing = drawboxWidth / 24;
	for(i = 0; i <= 24; i++)
	{
		[NSBezierPath strokeLineFromPoint:NSMakePoint(i * barSpacing + 1, drawboxBottom - 2)
								  toPoint:NSMakePoint(i * barSpacing + 1, drawboxBottom - 10)];
	}
}

-(NSArray*)positionSortDescriptors
{
	return [EMSGeneralUtils positionSortDescriptors];
}

#pragma mark Bindings stuff

+(void) initialize
{
    [self exposeBinding:@"contentSet"];
}

- (void)bind:(NSString*)bindingName
    toObject:(id)observableObject
 withKeyPath:(NSString*)observableKeyPath
     options:(NSDictionary*)options
{
    [observableObject addObserver:self
                       forKeyPath:observableKeyPath
                          options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
                          context:nil];
}

-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{
    NSArray *newArray = [[object valueForKeyPath:keyPath] sortedArrayUsingDescriptors:[self positionSortDescriptors]];
	[queuedItems setArray:newArray];
    [self setNeedsDisplay:YES];
}

@end
